home *** CD-ROM | disk | FTP | other *** search
- /**
- * Copyright (c) 2008, Jose Enrique Bolanos, Jorge Villalobos
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * * Neither the name of Jose Enrique Bolanos, Jorge Villalobos nor the names
- * of its contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
- * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- **/
-
- const Cc = Components.classes;
- const Ci = Components.interfaces;
- const Cr = Components.results;
- const Ce = Components.Exception;
-
- const CLASS_ID = Components.ID("{AE6C9422-FB46-4C0A-86D2-9C10A2E9B5CB}");
- const CLASS_NAME = "Fire.fm protocol";
- const CONTRACT_ID = "@mozilla.org/network/protocol;1?name=firefm";
-
- // Regular expression for the stations (station/type/id).
- const STATION_REGEX = /^firefm:(?:\/\/)?station\/([^\/]+)\/([^\/]+)/i;
-
- /**
- * Handles the firefm protocol
- */
- function fmProtocolHandler() {
- }
-
- fmProtocolHandler.prototype = {
-
- scheme : "firefm",
-
- defaultPort : -1,
-
- protocolFlags :
- (Ci.nsIProtocolHandler.URI_NORELATIVE | Ci.nsIProtocolHandler.URI_NOAUTH),
-
- allowPort: function(port, scheme) {
- return false;
- },
-
- newURI : function(aSpec, aCharset, aBaseURI) {
- let uri = Cc["@mozilla.org/network/simple-uri;1"].createInstance(Ci.nsIURI);
- uri.spec = aSpec;
-
- return uri;
- },
-
- newChannel : function(aInputURI) {
- let ioService =
- Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
- let regexResult = STATION_REGEX.exec(aInputURI.spec);
-
- if (regexResult && (3 == regexResult.length)) {
- let win =
- Cc['@mozilla.org/appshell/window-mediator;1'].
- getService(Ci.nsIWindowMediator).
- getMostRecentWindow("navigator:browser");
-
- let stationObj = win.FireFM.Station;
- let id = regexResult[2];
-
- switch (regexResult[1]) {
- case "artist":
- win.FireFMChrome.BrowserOverlay.verifyStation(
- id, stationObj.TYPE_ARTIST);
- break;
- case "user":
- win.FireFMChrome.BrowserOverlay.verifyStation(
- id, stationObj.TYPE_USER);
- break;
- case "tag":
- win.FireFMChrome.BrowserOverlay.verifyStation(
- id, stationObj.TYPE_TAG);
- break;
- case "recommended":
- stationObj.setStation(id, stationObj.TYPE_RECOMMENDED);
- stationObj.play();
- break;
- case "neighborhood":
- stationObj.setStation(id, stationObj.TYPE_NEIGHBORHOOD);
- stationObj.play();
- break;
- case "loved":
- stationObj.setStation(id, stationObj.TYPE_LOVED);
- stationObj.play();
- break;
- }
- }
-
- // XXX: Return "javascript:" to satisfy the method signature without
- // not actually loading anything in the browser.
- return ioService.newChannel("javascript:", null, null);
- },
-
- /**
- * The QueryInterface method provides runtime type discovery.
- * More: http://developer.mozilla.org/en/docs/nsISupports
- * @param aIID the IID of the requested interface.
- * @return the resulting interface pointer.
- */
- QueryInterface : function(aIID) {
- if (!aIID.equals(Ci.nsIProtocolHandler) &&
- !aIID.equals(Ci.nsISupports)) {
- throw Cr.NS_ERROR_NO_INTERFACE;
- }
-
- return this;
- }
- };
-
- /**
- * The nsIFactory interface allows for the creation of nsISupports derived
- * classes without specifying a concrete class type.
- * More: http://developer.mozilla.org/en/docs/nsIFactory
- */
- var fmProtocolHandlerFactory = {
- createInstance: function (aOuter, aIID) {
- if (null != aOuter) {
- throw Cr.NS_ERROR_NO_AGGREGATION;
- }
- return (new fmProtocolHandler()).QueryInterface(aIID);
- }
- };
-
- /**
- * The nsIModule interface must be implemented by each XPCOM component. It is
- * the main entry point by which the system accesses an XPCOM component.
- * More: http://developer.mozilla.org/en/docs/nsIModule
- */
- var fmProtocolHandlerModule = {
- /**
- * When the nsIModule is discovered, this method will be called so that any
- * setup registration can be preformed.
- * @param aCompMgr the global component manager.
- * @param aLocation the location of the nsIModule on disk.
- * @param aLoaderStr opaque loader specific string.
- * @param aType loader type being used to load this module.
- */
- registerSelf : function(aCompMgr, aLocation, aLoaderStr, aType) {
- aCompMgr.QueryInterface(Ci.nsIComponentRegistrar);
- aCompMgr.registerFactoryLocation(
- CLASS_ID, CLASS_NAME, CONTRACT_ID, aLocation, aLoaderStr, aType);
- },
-
- /**
- * When the nsIModule is being unregistered, this method will be called so
- * that any cleanup can be preformed.
- * @param aCompMgr the global component manager.
- * @param aLocation the location of the nsIModule on disk.
- * @param aLoaderStr opaque loader specific string.
- */
- unregisterSelf : function (aCompMgr, aLocation, aLoaderStr) {
- aCompMgr.QueryInterface(Ci.nsIComponentRegistrar);
- aCompMgr.unregisterFactoryLocation(CLASS_ID, aLocation);
- },
-
- /**
- * This method returns a class object for a given ClassID and IID.
- * @param aCompMgr the global component manager.
- * @param aClass the ClassID of the object instance requested.
- * @param aIID the IID of the object instance requested.
- * @return the resulting interface pointer.
- * @throws NS_ERROR_NOT_IMPLEMENTED if aIID is inadequate.
- * @throws NS_ERROR_NO_INTERFACE if the interface is not found.
- */
- getClassObject : function(aCompMgr, aClass, aIID) {
- if (!aIID.equals(Ci.nsIFactory)) {
- throw Cr.NS_ERROR_NOT_IMPLEMENTED;
- }
-
- if (aClass.equals(CLASS_ID)) {
- return fmProtocolHandlerFactory;
- }
-
- throw Cr.NS_ERROR_NO_INTERFACE;
- },
-
- /**
- * This method may be queried to determine whether or not the component
- * module can be unloaded by XPCOM.
- * @param aCompMgr the global component manager.
- * @return true if the module can be unloaded by XPCOM. false otherwise.
- */
- canUnload: function(aCompMgr) {
- return true;
- }
- };
-
- /**
- * Initial entry point.
- * @param aCompMgr the global component manager.
- * @param aFileSpec component file.
- * @return the module for the service.
- */
- function NSGetModule(aCompMgr, aFileSpec) {
- return fmProtocolHandlerModule;
- }
-